package com.wskeee.ec.components
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.utils.DisplayObjectUitl;

	public class ScrollBarBase extends TrackBase
	{
		public function ScrollBarBase(dir:String)
		{
			super(dir);
		}
		
		//========================= skinpark ====================
		private var currentArrow:Sprite;
		private var arrowDelayEnterFrameID:uint;
		override protected function initSkinPark():void
		{
			super.initSkinPark();
			upArrow.addEventListener(MouseEvent.MOUSE_DOWN,onArrowMouseDown);
			downArrow.addEventListener(MouseEvent.MOUSE_DOWN,onArrowMouseDown);
		}
		
		protected function onArrowMouseDown(event:MouseEvent):void
		{
			currentArrow = event.currentTarget as Sprite;
			stage.addEventListener(MouseEvent.MOUSE_UP,onArrowMouseUp);
			onArrowEnterFrame(null);
			arrowDelayEnterFrameID = setTimeout(function():void
			{
				if(currentArrow)
					stage.addEventListener(Event.ENTER_FRAME,onArrowEnterFrame);
			},500)
		}
		
		protected function onArrowMouseUp(event:MouseEvent):void
		{
			currentArrow = null;
			clearTimeout(arrowDelayEnterFrameID);
			stage.removeEventListener(Event.ENTER_FRAME,onArrowEnterFrame);
			stage.removeEventListener(MouseEvent.MOUSE_UP,onArrowMouseUp);
		}
		
		protected function onArrowEnterFrame(event:Event):void
		{
			if(!currentArrow.hitTestPoint(stage.mouseX,stage.mouseY,true))return;
			switch(currentArrow)
			{
				case upArrow:
					setValue(value-snapInterval);
					break;
				case downArrow:
					setValue(value+snapInterval);
					break;
			}
		}
		
		override protected function destroyTrackEvent():void
		{
			super.destroyTrackEvent();
			upArrow.removeEventListener(MouseEvent.MOUSE_DOWN,onArrowMouseDown);
			downArrow.removeEventListener(MouseEvent.MOUSE_DOWN,onArrowMouseDown);
		}
		//========================= end =========================
		
		//========================= invaidate ===================
		override protected function commitProperties():void
		{
			super.commitProperties();
			if(checkEnabledFlag)
			{
				checkEnabledFlag = false;
				enabled = !viewarea || (viewarea && viewarea[info.wh]<maxScrollPosition);
			}
		}
		override protected function measure():void
		{
			measureWidth = explicitWidth;
			measureHeight = explicitHeight;
			if(isNaN(explicitWidth))
				measureWidth = 0;
			if(isNaN(explicitHeight))
				measureHeight = 0;
			if(isNaN(explicitHeight) || isNaN(explicitWidth))
				setActualSize(measureWidth,measureHeight);
				
		}
		//========================= end =========================
		
		
		//========================= act =========================
		private var checkEnabledFlag:Boolean = false;
		protected function checkEnabled():void
		{
			checkEnabledFlag = true;
			invalidateProperties();
		}
		//---------------------
		//onTargetSizeChange
		//当 viewarea 或者 target 改变时调用 
		//---------------------
		protected function onTargetSizeChange(evt:Event = null):void
		{
			if(target)
			{
				var rect:Rectangle = target is EComponent ? EComponent(target).getFullBounds() : DisplayObjectUitl.getFullBounds(target);
				maxScrollPosition = rect[info.wh]+2;
				if(evt && !viewarea)
					target = target;
				onChange();
			}
		}
		
		//---------------------
		//onTargetMouseWheel
		//---------------------
		protected function onTargetMouseWheel(event:MouseEvent):void
		{
			//
		}
		//========================= end =========================
		
		
		
		
		//========================= properties ==================
		//---------------------
		//scrollPosition
		//---------------------
		protected var _scrollPosition:Number = 0;
		/**
		 * 设置当前滚动值，0 ~ maxScrollPosition
		 * @param value
		 * 
		 */
		public function get scrollPosition():Number
		{
			return value;
		}
		
		
		public function set scrollPosition(value:Number):void
		{
			setValue(value);
		}
		
		//---------------------
		//maxScrollPosition
		//---------------------
		//最大滚动值，当滚动对象 高度/宽动 改变时应即时修改该值以正确显示对应比例
		protected var _maxScrollPosition:Number = 0;
		/**
		 * 最大滚动值
		 * @return 
		 * 
		 */
		public function get maxScrollPosition():Number
		{
			return maximum;
		}
		
		public function set maxScrollPosition(value:Number):void
		{
			maximum = value;
			checkEnabled();
		}
		
		//---------------------
		//target
		//---------------------
		protected var _target:DisplayObject;
		/**
		 * 设置滚动目标 
		 * @param value
		 * 
		 */
		public function set target(value:DisplayObject):void
		{
			if(_target)
			{
				_target.removeEventListener(Event.RESIZE,onTargetSizeChange);
				_target.removeEventListener(MouseEvent.MOUSE_WHEEL,onTargetMouseWheel);
				_target.scrollRect = null;
			}
				
			_target = value;
			if(_target)
			{
				_target.addEventListener(Event.RESIZE,onTargetSizeChange);
				_target.addEventListener(MouseEvent.MOUSE_WHEEL,onTargetMouseWheel);
			}
				
			onTargetSizeChange();
		}
		
		public function get target():DisplayObject
		{
			return _target;
		}
		
		//---------------------
		//viewarea
		//---------------------
		protected var _viewarea:Rectangle;
		/**
		 * 设置滚动可视区域 
		 * @param value
		 * 
		 */
		public function set viewarea(value:Rectangle):void
		{
			if(value && _viewarea && value.x == _viewarea.x && value.y == viewarea.y && value.width == viewarea.width && value.height == viewarea.height)
				return;
			if(value && (isNaN(value.width) || value.width==0 || isNaN(value.height) || value.height==0))return;
			_viewarea = value;
			onTargetSizeChange();
			checkEnabled();
		}
		public function get viewarea():Rectangle
		{
			return _viewarea;
		}
		
		//---------------------
		//pageSize
		//---------------------
		private var _pageSize:Number;
		/**
		 * 按下 track 所滚动的值 
		 * @param value
		 * 
		 */
		public function set pageSize(value:Number):void
		{
			_pageSize = value;
			invalidateProperties();
		}
		public function get pageSize():Number
		{
			return _pageSize;
		}
		
		//---------------------
		//hideArrow
		//---------------------
		private var _hideArrow:Boolean = false;
		/**
		 * 隐藏上下控制器 
		 * @param value
		 * 
		 */
		public function set hideArrow(value:Boolean):void
		{
			_hideArrow = value;
			invalidateProperties();
		}
		public function get hideArrow():Boolean
		{
			return _hideArrow;
		}
		//========================= end =========================
		
		//========================= skin ========================
		protected function get upArrow():Sprite
		{
			return Object(skinDrawer).upArrow;
		}
		
		protected function get downArrow():Sprite
		{
			return Object(skinDrawer).downArrow;
		}
		//========================= end =========================
	}
}